home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Gray Matter (PalmOS) / GraySwitch / Src / Starter.c next >
Text File  |  2000-06-23  |  11KB  |  464 lines

  1. /***********************************************************************
  2.  *
  3.  * Copyright (c) 1994-1999 3Com Corporation or its subsidiaries.
  4.  * All rights reserved.
  5.  *
  6.  * PROJECT:  Pilot
  7.  * FILE:     Starter.c
  8.  * AUTHOR:   Roger Flores: May 20, 1997
  9.  *
  10.  * DECLARER: Starter
  11.  *
  12.  * DESCRIPTION:
  13.  *      
  14.  *
  15.  **********************************************************************/
  16. #include <Pilot.h>
  17. #include <ScrDriverNew.h>
  18. #include <SysEvtMgr.h>
  19. #include "StarterRsc.h"
  20.  
  21.  
  22.  
  23. /***********************************************************************
  24.  *
  25.  *   Entry Points
  26.  *
  27.  ***********************************************************************/
  28.  
  29.  
  30. /***********************************************************************
  31.  *
  32.  *   Internal Structures
  33.  *
  34.  ***********************************************************************/
  35. typedef struct 
  36.     {
  37.     Byte replaceme;
  38.     } StarterPreferenceType;
  39.  
  40. typedef struct 
  41.     {
  42.     Byte replaceme;
  43.     } StarterAppInfoType;
  44.  
  45. typedef StarterAppInfoType* StarterAppInfoPtr;
  46.  
  47.  
  48. /***********************************************************************
  49.  *
  50.  *   Global variables
  51.  *
  52.  ***********************************************************************/
  53. //static Boolean HideSecretRecords;
  54.  
  55.  
  56. /***********************************************************************
  57.  *
  58.  *   Internal Constants
  59.  *
  60.  ***********************************************************************/
  61. #define appFileCreator                    'strt'
  62. #define appVersionNum              0x01
  63. #define appPrefID                  0x00
  64. #define appPrefVersionNum          0x01
  65.  
  66.  
  67. // Define the minimum OS version we support
  68. #define ourMinVersion    sysMakeROMVersion(2,0,0,sysROMStageRelease,0)
  69.  
  70.  
  71. /***********************************************************************
  72.  *
  73.  *   Internal Functions
  74.  *
  75.  ***********************************************************************/
  76.  
  77.  
  78. /***********************************************************************
  79.  *
  80.  * FUNCTION:    RomVersionCompatible
  81.  *
  82.  * DESCRIPTION: This routine checks that a ROM version is meet your
  83.  *              minimum requirement.
  84.  *
  85.  * PARAMETERS:  requiredVersion - minimum rom version required
  86.  *                                (see sysFtrNumROMVersion in SystemMgr.h 
  87.  *                                for format)
  88.  *              launchFlags     - flags that indicate if the application 
  89.  *                                UI is initialized.
  90.  *
  91.  * RETURNED:    error code or zero if rom is compatible
  92.  *
  93.  * REVISION HISTORY:
  94.  *
  95.  ***********************************************************************/
  96. static Err RomVersionCompatible(DWord requiredVersion, Word launchFlags)
  97. {
  98.     DWord romVersion;
  99.  
  100.     // See if we're on in minimum required version of the ROM or later.
  101.     FtrGet(sysFtrCreator, sysFtrNumROMVersion, &romVersion);
  102.     if (romVersion < requiredVersion)
  103.         {
  104.         if ((launchFlags & (sysAppLaunchFlagNewGlobals | sysAppLaunchFlagUIApp)) ==
  105.             (sysAppLaunchFlagNewGlobals | sysAppLaunchFlagUIApp))
  106.             {
  107.             FrmAlert (RomIncompatibleAlert);
  108.         
  109.             // Pilot 1.0 will continuously relaunch this app unless we switch to 
  110.             // another safe one.
  111.             if (romVersion < sysMakeROMVersion(2,0,0,sysROMStageRelease,0))
  112.                 AppLaunchWithCommand(sysFileCDefaultApp, sysAppLaunchCmdNormalLaunch, NULL);
  113.             }
  114.         
  115.         return (sysErrRomIncompatible);
  116.         }
  117.  
  118.     return (0);
  119. }
  120.  
  121.  
  122. /***********************************************************************
  123.  *
  124.  * FUNCTION:    GetObjectPtr
  125.  *
  126.  * DESCRIPTION: This routine returns a pointer to an object in the current
  127.  *              form.
  128.  *
  129.  * PARAMETERS:  formId - id of the form to display
  130.  *
  131.  * RETURNED:    VoidPtr
  132.  *
  133.  * REVISION HISTORY:
  134.  *
  135.  *
  136.  ***********************************************************************/
  137. static VoidPtr GetObjectPtr(Word objectID)
  138.     {
  139.     FormPtr frmP;
  140.  
  141.  
  142.     frmP = FrmGetActiveForm();
  143.     return (FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP, objectID)));
  144. }
  145.  
  146.  
  147. /***********************************************************************
  148.  *
  149.  * FUNCTION:    MainFormInit
  150.  *
  151.  * DESCRIPTION: This routine initializes the MainForm form.
  152.  *
  153.  * PARAMETERS:  frm - pointer to the MainForm form.
  154.  *
  155.  * RETURNED:    nothing
  156.  *
  157.  * REVISION HISTORY:
  158.  *
  159.  *
  160.  ***********************************************************************/
  161. static void MainFormInit(FormPtr frmP)
  162. {
  163. }
  164.  
  165.  
  166. /***********************************************************************
  167.  *
  168.  * FUNCTION:    MainFormDoCommand
  169.  *
  170.  * DESCRIPTION: This routine performs the menu command specified.
  171.  *
  172.  * PARAMETERS:  command  - menu item id
  173.  *
  174.  * RETURNED:    nothing
  175.  *
  176.  * REVISION HISTORY:
  177.  *
  178.  *
  179.  ***********************************************************************/
  180. static Boolean MainFormDoCommand(Word command)
  181. {
  182.     Boolean handled = false;
  183.  
  184.  
  185.     switch (command)
  186.         {
  187.         case MainOptionsAboutStarterApp:
  188.             MenuEraseStatus (0);
  189.             AbtShowAbout (appFileCreator);
  190.             handled = true;
  191.             break;
  192.  
  193.         }
  194.     return handled;
  195. }
  196.  
  197. #define   sysFtrNumDisplayDepth   7               // Display depth
  198.  // Result is the "default" display depth for the screen.
  199.  // This value is used by ScrDisplayMode when setting the default display depth.
  200.  
  201. //-----------------------------------------------------------------------
  202. // FUNCTION:   SetDefaultDepth
  203. // DESCRIPTION:   Set the default depth for the system
  204. // PARAMETERS:   New default depth, pass 1 or 2 for that depth.
  205. //-----------------------------------------------------------------------
  206. static Err SetDefaultDepth(DWord newDefaultDepth)
  207. {
  208.     Err err = FtrSet(sysFtrCreator, sysFtrNumDisplayDepth, newDefaultDepth);
  209.     if (!err)
  210.     err = ScrDisplayMode(scrDisplayModeSetToDefaults, NULL, NULL, NULL, NULL);
  211.     return err ;
  212. }
  213.  
  214. /***********************************************************************
  215.  *
  216.  * FUNCTION:    MainFormHandleEvent
  217.  *
  218.  * DESCRIPTION: This routine is the event handler for the 
  219.  *              "MainForm" of this application.
  220.  *
  221.  * PARAMETERS:  eventP  - a pointer to an EventType structure
  222.  *
  223.  * RETURNED:    true if the event has handle and should not be passed
  224.  *              to a higher level handler.
  225.  *
  226.  * REVISION HISTORY:
  227.  *
  228.  *
  229.  ***********************************************************************/
  230. static Boolean MainFormHandleEvent(EventPtr eventP)
  231. {
  232.     Boolean handled = false;
  233.     FormPtr frmP;
  234.  
  235.  
  236.     switch (eventP->eType) 
  237.         {
  238.         case menuEvent:
  239.             return MainFormDoCommand(eventP->data.menu.itemID);
  240.  
  241.         case frmOpenEvent:
  242.             frmP = FrmGetActiveForm();
  243.             MainFormInit( frmP);
  244.             FrmDrawForm ( frmP);
  245.             SetDefaultDepth(2);
  246.             handled = true;
  247.             break;
  248.  
  249.         default:
  250.             break;
  251.         
  252.         }
  253.     
  254.     return handled;
  255. }
  256.  
  257.  
  258. /***********************************************************************
  259.  *
  260.  * FUNCTION:    AppHandleEvent
  261.  *
  262.  * DESCRIPTION: This routine loads form resources and set the event
  263.  *              handler for the form loaded.
  264.  *
  265.  * PARAMETERS:  event  - a pointer to an EventType structure
  266.  *
  267.  * RETURNED:    true if the event has handle and should not be passed
  268.  *              to a higher level handler.
  269.  *
  270.  * REVISION HISTORY:
  271.  *
  272.  *
  273.  ***********************************************************************/
  274. static Boolean AppHandleEvent( EventPtr eventP)
  275. {
  276.     Word formId;
  277.     FormPtr frmP;
  278.  
  279.  
  280.     if (eventP->eType == frmLoadEvent)
  281.         {
  282.         // Load the form resource.
  283.         formId = eventP->data.frmLoad.formID;
  284.         frmP = FrmInitForm(formId);
  285.         FrmSetActiveForm(frmP);
  286.  
  287.         // Set the event handler for the form.  The handler of the currently
  288.         // active form is called by FrmHandleEvent each time is receives an
  289.         // event.
  290.         switch (formId)
  291.             {
  292.             case MainForm:
  293.                 FrmSetEventHandler(frmP, MainFormHandleEvent);
  294.                 break;
  295.  
  296.             default:
  297. //                ErrFatalDisplay("Invalid Form Load Event");
  298.                 break;
  299.  
  300.             }
  301.         return true;
  302.         }
  303.     
  304.     return false;
  305. }
  306.  
  307.  
  308. /***********************************************************************
  309.  *
  310.  * FUNCTION:    AppEventLoop
  311.  *
  312.  * DESCRIPTION: This routine is the event loop for the application.  
  313.  *
  314.  * PARAMETERS:  nothing
  315.  *
  316.  * RETURNED:    nothing
  317.  *
  318.  * REVISION HISTORY:
  319.  *
  320.  *
  321.  ***********************************************************************/
  322. static void AppEventLoop(void)
  323. {
  324.     Word error;
  325.     EventType event;
  326.  
  327.  
  328.     do {
  329.         EvtGetEvent(&event, evtWaitForever);
  330.         
  331.         
  332.         if (! SysHandleEvent(&event))
  333.             if (! MenuHandleEvent(0, &event, &error))
  334.                 if (! AppHandleEvent(&event))
  335.                     FrmDispatchEvent(&event);
  336.  
  337.     } while (event.eType != appStopEvent);
  338. }
  339.  
  340.  
  341. /***********************************************************************
  342.  *
  343.  * FUNCTION:     AppStart
  344.  *
  345.  * DESCRIPTION:  Get the current application's preferences.
  346.  *
  347.  * PARAMETERS:   nothing
  348.  *
  349.  * RETURNED:     Err value 0 if nothing went wrong
  350.  *
  351.  * REVISION HISTORY:
  352.  *
  353.  *
  354.  ***********************************************************************/
  355. static Err AppStart(void)
  356. {
  357.     StarterPreferenceType prefs;
  358.     Word prefsSize;
  359.  
  360.  
  361.     // Read the saved preferences / saved-state information.
  362.     prefsSize = sizeof(StarterPreferenceType);
  363.     if (PrefGetAppPreferences(appFileCreator, appPrefID, &prefs, &prefsSize, true) != 
  364.         noPreferenceFound)
  365.         {
  366.         }
  367.     
  368.    return 0;
  369. }
  370.  
  371.  
  372. /***********************************************************************
  373.  *
  374.  * FUNCTION:    AppStop
  375.  *
  376.  * DESCRIPTION: Save the current state of the application.
  377.  *
  378.  * PARAMETERS:  nothing
  379.  *
  380.  * RETURNED:    nothing
  381.  *
  382.  * REVISION HISTORY:
  383.  *
  384.  *
  385.  ***********************************************************************/
  386. static void AppStop(void)
  387. {
  388.    StarterPreferenceType prefs;
  389.    
  390.    
  391.     // Write the saved preferences / saved-state information.  This data 
  392.     // will be backed up during a HotSync.
  393.     PrefSetAppPreferences (appFileCreator, appPrefID, appPrefVersionNum, 
  394.         &prefs, sizeof (prefs), true);
  395. }
  396.  
  397.  
  398.  
  399. /***********************************************************************
  400.  *
  401.  * FUNCTION:    StarterPilotMain
  402.  *
  403.  * DESCRIPTION: This is the main entry point for the application.
  404.  * PARAMETERS:  cmd - word value specifying the launch code. 
  405.  *              cmdPB - pointer to a structure that is associated with the launch code. 
  406.  *              launchFlags -  word value providing extra information about the launch.
  407.  *
  408.  * RETURNED:    Result of launch
  409.  *
  410.  * REVISION HISTORY:
  411.  *
  412.  *
  413.  ***********************************************************************/
  414. static DWord StarterPilotMain(Word cmd, Ptr cmdPBP, Word launchFlags)
  415. {
  416.     Err error;
  417.  
  418.  
  419.     error = RomVersionCompatible (ourMinVersion, launchFlags);
  420.     if (error) return (error);
  421.  
  422.  
  423.     switch (cmd)
  424.         {
  425.         case sysAppLaunchCmdNormalLaunch:
  426.             error = AppStart();
  427.             if (error) 
  428.                 return error;
  429.                 
  430.             FrmGotoForm(MainForm);
  431.             AppEventLoop();
  432.             AppStop();
  433.             break;
  434.  
  435.         default:
  436.             break;
  437.  
  438.         }
  439.     
  440.     return 0;
  441. }
  442.  
  443.  
  444. /***********************************************************************
  445.  *
  446.  * FUNCTION:    PilotMain
  447.  *
  448.  * DESCRIPTION: This is the main entry point for the application.
  449.  *
  450.  * PARAMETERS:  cmd - word value specifying the launch code. 
  451.  *              cmdPB - pointer to a structure that is associated with the launch code. 
  452.  *              launchFlags -  word value providing extra information about the launch.
  453.  * RETURNED:    Result of launch
  454.  *
  455.  * REVISION HISTORY:
  456.  *
  457.  *
  458.  ***********************************************************************/
  459. DWord PilotMain( Word cmd, Ptr cmdPBP, Word launchFlags)
  460. {
  461.     return StarterPilotMain(cmd, cmdPBP, launchFlags);
  462. }
  463.  
  464.